package com.bjpowernode.boss.mvc.service.Impl;

import java.math.BigDecimal;
import java.util.Date;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.boss.common.enums.PublicStatus;
import com.bjpowernode.boss.common.enums.ResponseCode;
import com.bjpowernode.boss.common.expection.BossExpection;
import com.bjpowernode.boss.common.utils.NoUtil;
import com.bjpowernode.boss.dao.entity.UserAccount;
import com.bjpowernode.boss.dao.entity.UserInfo;
import com.bjpowernode.boss.dao.mapper.UserAccountMapper;
import com.bjpowernode.boss.dao.mapper.UserInfoMapper;
import com.bjpowernode.boss.mvc.models.form.user.UserEditForm;
import com.bjpowernode.boss.mvc.service.UserService;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Override
    public IPage<UserInfo> pageList(Integer pageNo, Integer pageSize) {

        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        return userInfoMapper.selectPage(Page.of(pageNo, pageSize), wrapper);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void editUser(UserEditForm editForm) {
        if (StrUtil.isNotBlank(editForm.getId())) {
            UserInfo edit = new UserInfo();
            edit.setUserEmail(editForm.getUserEmail());
            edit.setPhone(editForm.getPhone());
            edit.setShopName(editForm.getShopName());
            edit.setShopScope(editForm.getShopScope());
            edit.setId(editForm.getId());
            userInfoMapper.updateById(edit);
        } else {
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("login_name", editForm.getLoginName());
            UserInfo userInfo = userInfoMapper.selectOne(wrapper);


            if (userInfo != null) {
                throw new BossExpection(ResponseCode.USER_LOGIN_NAME_EXISTS);
            } else {

                //创建新商户
                userInfo = new UserInfo();
                userInfo.setId(NoUtil.getUUID());
                userInfo.setUserNo(NoUtil.getUserNo());
                userInfo.setLoginName(editForm.getLoginName());
                userInfo.setAccountNo(NoUtil.getAccountNo());
                userInfo.setPhone(editForm.getPhone());
                userInfo.setPassword(NoUtil.getUUID());
                userInfo.setPayPwd(NoUtil.getUUID());
                userInfo.setShopName(editForm.getShopName());
                userInfo.setShopScope(editForm.getShopScope());
                userInfo.setUserEmail(editForm.getUserEmail());
                userInfo.setStatus(PublicStatus.DISABLE.name());
                userInfo.setCreateTime(new Date());
                userInfo.setRemark("创建商户");
                userInfoMapper.insert(userInfo);

                //创建Account
                UserAccount account = new UserAccount();
                account.setId(NoUtil.getUUID());
                account.setUserNo(userInfo.getUserNo());
                account.setAccountNo(userInfo.getAccountNo());
                account.setBalance(new BigDecimal("0"));
                account.setTotalIncome(new BigDecimal("0"));
                account.setTotalExpend(new BigDecimal("0"));
                account.setStatus(PublicStatus.DISABLE.name());
                account.setRemark("新用户创建资金账号");
                account.setEditTime(new Date());
                userAccountMapper.insert(account);
            }
        }
    }

    @Override
    public UserInfo queryById(String userId) {
        return userInfoMapper.selectById(userId);
    }

    @Override
    public void editStatus(String userNo) {
        UserInfo userInfo = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("user_no", userNo));

        if (userInfo == null) {
            throw new BossExpection(ResponseCode.USER_NOT_EXISTS);
        }
        String editStatus = PublicStatus.ENABLE.name();
        String status = userInfo.getStatus();
        if (PublicStatus.ENABLE.name().equals(status)) {
            editStatus = PublicStatus.DISABLE.name();
        }
        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", userInfo.getId()).set("status", editStatus);
        userInfoMapper.update(updateWrapper);

    }
}
